<?php

namespace app\common\model;

class Ad extends Base
{
    /**
     * [getAdCountByCondition  根据搜索条件获取满足条数]
     * @author [南風]
     * @param $map
     * @return int|string
     */
    public function getAdCountByCondition($map)
    {
        return $this->where($map)->count();
    }

    /**
     * [getAdByCondition  广告]
     * @author [南風]
     * @param $map
     * @param $from
     * @param $size
     * @return array
     */
    public function getAdByCondition($map, $from, $size)
    {

        $lists = $this->field('trc_ad.*,name')
                    ->join('trc_ad_position', 'trc_ad.ad_position_id = trc_ad_position.id')
                    ->where($map)
                     ->limit($from, $size)
                    ->order('sort asc,id asc')
                    ->select();

        return $lists;
    }

    /**
     * 插入信息
     * @param $param
     */
    public function insertAd($param)
    {
        $this->allowField(true)->save($param);
        return $this->id;
    }

    /**
     * 编辑信息
     * @param $param
     */
    public function editAd($param)
    {
        return $result = $this->allowField(true)->save($param, ['id' => $param['id']]);
    }


    /**
     * [getBanner  获取首页轮播图片]
     * @author [南風]
     * @param $map
     * @param $size
     * @return array
     */
    public function getBanner($map = [], $size=5)
    {
        return $this->where($map)->order('orderby desc,id desc')->limit($size)->select();
    }
    /**
     * [getBanner  获取轮播图片]
     * @author [南風]
     * @param $map
     * @param $size
     * @return array
     */
    public function banner($ad=1, $size=5)
    {
        $map["ad_position_id"] = $ad;
        $map["status"] = 1;

        return $this->where($map)->order('sort asc')->limit($size)->select();
    }
}